﻿using ImageManager.Common;

namespace ImageManager.DBAccess
{
    public class DbConfig
    {
        internal string server { get; private set; }
        internal string uid { get; private set; }
        internal string pwd { get; private set; }
        internal int port { get; private set; }
        internal string database { get; private set; }
        public DbConfig(string server, string uid, string pwd, string dbname,int port = 1433)
        {
            this.server = server;
            this.uid = uid;
            this.pwd = pwd;
            this.port = port;
            this.database = dbname;
        }
        public static DbConfig Default()
        {
            var server = ConfigReader.ReadSetting("DbConnection", "Server");
            var uid = ConfigReader.ReadSetting("DbConnection", "Uid");
            var pwd = ConfigReader.ReadSetting("DbConnection", "Pwd");
            var p = ConfigReader.ReadSetting("DbConnection", "Port");
            var dbname = ConfigReader.ReadSetting("DbConnection", "Database");
            var _port = 0;
            var port = 1433;
            if (int.TryParse(p, out _port)) port = _port;
            return new DbConfig(server,uid, pwd, dbname,port);
        }
        public void Test()
        {
            DbInstance dbInstance = DbInstance.CreateDefaultInstance();
            dbInstance.Open();
            dbInstance.Close();
        }
    }
}
